<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Http\Services\Manage\ExportService;
use App\Repositories\Manage\StatRepository;

class StatController extends ManageBaseController
{

    private $statRepo = NULL,
        $start_time = NULL,
        $end_time = NULL;

    public function __construct(
        StatRepository $statRepo
    )
    {
        parent::__construct();
        $this->statRepo = $statRepo;

        $this->start_time = request()->post("start_time", date("Y-m-d", strtotime("-1 month")));
        $this->end_time = request()->post("end_time", date("Y-m-d"));
    }

    public function shop()
    {
        $filter = $this->_filter();
        $res = $this->statRepo->statShop($filter['start_time'], $filter['end_time'], $filter);
        return retSuc(['data' => $res]);
    }

    /**
     * 商品销量
     */
    public function product()
    {
        $filter = $this->_filter();
        $res = $this->statRepo->getProductStat($filter['start_time'], $filter['end_time'], $filter);
        return retSuc(['data' => $res]);
    }

    /**
     * 档口统计
     */
    public function stall()
    {
        $filter = $this->_filter();
        $res = $this->statRepo->statStall($filter['start_time'], $filter['end_time'], $filter);
        return retSuc(['data' => $res]);
    }

    public function export()
    {
        $type = request()->get("type", "");
        if (!in_array($type, ['shop', 'stall', 'product'])) {
            return retFail("导出类型错误");
        }

        $filter = $this->_filter();
        $filter['isR'] = 1;
        switch ($type) {
            case "shop":
                $res = $this->statRepo->statShop($filter['start_time'], $filter['end_time'], $filter);
                $keys["shop_title"] = "店铺";
                $keys["total_price"] = "总交易额";
                $keys["amount"] = "总单量";
                $keys["product_price"] = "商品总金额";
                $keys["bag_price"] = "总打包费";
                $keys["express_price"] = "总配送费";
                $keys["cdate"] = "日期";
                return (new  ExportService())->download($res, $keys, "shop");

                break;
            case "stall":
                $res = $this->statRepo->statStall($filter['start_time'], $filter['end_time'], $filter);

                $keys = [];
                $keys["shop_title"] = "店铺名称";
                $keys["stall_title"] = "档口名称";
                $keys["amount"] = "总单量";
                $keys["total_price"] = "商品总金额";
                $keys["total_bag_price"] = "总打包费";
                $keys["cdate"] = "日期";
                return (new  ExportService())->download($res, $keys, "stall");
                break;
            case "product":
                $res = $this->statRepo->getProductStat($filter['start_time'], $filter['end_time'], $filter);

                $keys = [];
                $keys["shop_title"] = "店铺名称";
                $keys["stall_title"] = "档口名称";
                $keys["category_title"] = "分类名称";
                $keys["product_title"] = "商品名称";
                $keys["amount"] = "销售数量";
                $keys["product_price"] = "商品售价";
                $keys["cdate"] = "日期";
                return (new  ExportService())->download($res, $keys, "product");
                break;
            default:

                break;
        }

        return retSuc([], "导出成功");
    }


    private function _filter()
    {
        $params = request()->all();

        $filter = [];
        if (!empty($params['create_time'])) {
            $filter['start_time'] = $params['create_time'][0];
            $filter['end_time'] = $params['create_time'][1];
        } else {
            $filter['start_time'] = request()->get("start_time", date("Y-m-d", strtotime("-1 month"))) . " 00:00:00";
            $filter['end_time'] = request()->get("end_time", date("Y-m-d")) . " 23:59:59";
        }

        $filter['skey'] = trim($params["keywords"] ?? "");
        $filter['stall_title'] = trim($params["stall_title"] ?? "");
        $filter['scate_title'] = trim($params["scate_title"] ?? "");
        $filter['shop_id'] = intval($params["shop_id"] ?? 0);

        if (!checkIsSuper(UID)) {
            $filter['shop_id'] = getUserRelateShopId(UID);
        }

        return $filter;
    }


}
